package cn.itsource.pethome.product.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.domain.ProductDetail;
import cn.itsource.pethome.product.mapper.ProductDetailMapper;
import cn.itsource.pethome.product.mapper.ProductMapper;
import cn.itsource.pethome.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void save(Product product) {
        ProductDetail productDetail = product.getProductDetail();
        //判断是否传入了id
        if (product.getId()==null){
            //没有  则添加服务
            productMapper.save(product);
            //设置productDetail中的product字段
            productDetail.setProduct(product);
            //添加服务详细
            productDetailMapper.save(productDetail);
        }
        //有则修改
        else {
            productMapper.update(product);
            //设置productDetail中的product字段
            productDetail.setProduct(product);
            productDetailMapper.update(productDetail);
        }
    }

    @Override
    public void delete(Long id) {
        //删除服务产品信息
        super.delete(id);
        //删除
        productDetailMapper.delete(id);
    }

    @Override
    public void batchRemove(List<Product> products) {
        //批量删除服务产品
        super.batchRemove(products);
        //批量删除相关明细
        productDetailMapper.batchremoveByproducts(products);
    }

    @Override
    public void batchOnSale(List<Product> products) {
        productMapper.batchOnSale(products);
    }

    @Override
    public void batchOffSale(List<Product> products) {
        productMapper.batchOffSale(products);

    }
}
